package com.kkw.shoppingmall.ware.config;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Kevin
 * @date 2021-06-18
 **/
@Configuration
public class MyMQConfig {

//    @RabbitListener(queues = "stockDelayQueue")
//    public void listener(Channel channel, Message message) throws IOException {
////        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }



    @Bean
    public Exchange stockEventExchange() {
        /**
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         */
        return new TopicExchange("stock-event-exchange", true, false);
    }

    /**
     * 容器中的Binding Queue Exchange都会自动创建（Rabbitmq没有的情况下)
     * 一旦创建好队列以后，即使队列里面的属性更改了 他也不会重新再次覆盖创建
     * @return
     */
    @Bean
    public Queue stockDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //死信交换机
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        //死信路由键
        arguments.put("x-dead-letter-routing-key", "stock.release");
        // 消息过期时间 2分钟
        arguments.put("x-message-ttl", 600000);
        Queue orderDelayQueue = new Queue("stockDelayQueue", true, false, false,arguments);

        return orderDelayQueue;
    }

    @Bean
    public Queue stockReleaseStockQueue() {
        Queue orderReleaseOrderQueue = new Queue("stockReleaseStockQueue", true, false, false);

        return orderReleaseOrderQueue;
    }


    @Bean
    public Binding stockReleaseBingDing() {
        /**
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding("stockReleaseStockQueue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",
                null);
    }

    @Bean
    public Binding stockLockedBingDing() {
        return new Binding("stockDelayQueue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }
}

